package cn.topevery.garden.wapi.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.topevery.garden.dal.logic.PersonLogic;
import cn.topevery.garden.entity.vo.PersonVO;
import hw.topevery.basis.dal.logic.SysUserLogic;
import hw.topevery.basis.entity.dto.SysUserDto;
import hw.topevery.basis.entity.model.UserInfo;
import hw.topevery.basis.entity.po.SysUser;
import hw.topevery.basis.runtime.UserRuntime;
import hw.topevery.framework.web.HttpResultCode;
import hw.topevery.framework.web.JsonResult;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/user")
@Api(tags = "用户信息")
public class UserController {
    @Autowired
    SysUserLogic sysUserLogic;

    @Autowired
    PersonLogic personLogic;

    @GetMapping("getUserById")
    public JsonResult<SysUser> getUserById(String userId) {
        JsonResult<SysUser> jsonResult = new JsonResult<>();
        try {
            jsonResult.data = sysUserLogic.getEntityByDataId(userId);
            jsonResult.setCode(HttpResultCode.SUCCESS.getCode());
            jsonResult.msg = HttpResultCode.SUCCESS.getMsg();
        } catch (Exception e) {
            jsonResult.setCode(400);
            jsonResult.success = false;
            jsonResult.msg = e.getMessage();
            log.error(e.getMessage(), e);
        }
        return jsonResult;
    }

    @GetMapping("/personInfo")
    public JsonResult<PersonVO> getPersonInfo() {
       /* UserRuntime userObject = UserRuntimeHolder.getCurrent();
        PersonVO person = personLogic.getByUserId(userObject.getUserId());
        return JsonResult.ok(person);*/
        return null;
    }

    @GetMapping("/userInfo")
    public JsonResult<UserInfo> getUserInfo() {
        JsonResult<UserInfo> res = new JsonResult<UserInfo>(true);
        UserInfo userInfo = new UserInfo();
/*        UserRuntime userObject = UserRuntimeHolder.getCurrent();
        if (userObject != null) {
            //太卡了先注释掉
            SysUser userVal = userObject.getUserVal();
            SysUserDto sysUserDto = new SysUserDto();
            BeanUtils.copyProperties(userVal,sysUserDto);
            userInfo.setInfo(sysUserDto);
            res.data = userInfo;
        } else {
            res.success = false;
            res.msg = "登录超时，请重新登录！";
        }*/
        return res;
    }

    @GetMapping(value = "/updateUserOpenId")
    public JsonResult<?> updateUserOpenId(String userId, String openId) {
        SysUser sysUser = sysUserLogic.getEntityByDataId(userId);
        sysUser.wxMaOpenId = openId;
        sysUserLogic.update(userId, sysUser);
        return JsonResult.ok();
    }
}
